home *** CD-ROM | disk | FTP | other *** search
/ Amiga News 95 / Amiga News 95.iso / dpat / dpat31 / iobject / demo_fr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-23  |  8.6 KB  |  402 lines

  1. /* Essai des objets V1.00
  2.  * Tue Mar 23 15:55:19 1993
  3.  * tabs = 2
  4.  */
  5.  
  6.  
  7. #define  INTUITION_PREFERENCES_H 0
  8. #include <intuition/intuition.h>
  9. #include <exec/memory.h>
  10. #include "IObject.h"
  11.  
  12. /* declarations des libraries utilisés */
  13. CPTR IntuitionBase=NULL;
  14. CPTR GfxBase=NULL;
  15. CPTR DiskfontBase=NULL;
  16.  
  17. struct Window *window=NULL;
  18.  
  19. struct NewWindow new_window=
  20. {
  21.     50, 25,      /* Position de la fenêtre */
  22.     480, 125,    /* Taille de la fenêtre */
  23.     0, 1,
  24.     CLOSEWINDOW|IDCMP_CYCLE|IDCMP_CHECK|IDCMP_STRING|IDCMP_BUTTON|IDCMP_STRING|
  25.     IDCMP_INTEGER|IDCMP_SCROLLER|VANILLAKEY,   /* IDCMPFlags */
  26.     SMART_REFRESH|WINDOWCLOSE|WINDOWDRAG|WINDOWDEPTH|ACTIVATE|RMBTRAP,    /* Flags*/
  27.     NULL,
  28.     NULL,
  29.     "Test de IObject.lib", /* Titre de la fenetre */
  30.     NULL,
  31.     NULL,
  32.     0, 0,
  33.     0, 0,
  34.     WBENCHSCREEN   /* Type: fenetre Workbench. */
  35. };
  36.  
  37. /* definitions des fontes utilisés */
  38. struct TextAttr font=
  39. {
  40.     "topaz.font",
  41.     8,
  42.     FS_NORMAL,
  43.     FPF_ROMFONT
  44. };
  45.  
  46. struct TextAttr opalfont=
  47. {
  48.     "opal.font",
  49.     9,
  50.     FS_NORMAL,
  51.     FPF_DISKFONT
  52. };
  53.  
  54.  
  55. /* identificateur pour les IObject */
  56. #define ID_CYCLE            0
  57. #define ID_SCROLLER        1
  58. #define ID_BUTTON            2
  59. #define ID_CHECK            3
  60. #define ID_INTEGER        4
  61. #define ID_STRING            5
  62. #define ID_TGBUTTON        6
  63. #define ID_IMGBUTTON    7
  64.  
  65. #define ID_MIN            ID_CYCLE
  66. #define ID_MAX            ID_IMGBUTTON
  67.  
  68. STRPTR text[]=    /* tableau de chaines pour le cycle */
  69. {
  70.     "IObject.lib",
  71.     "is very",
  72.     "useful.",
  73.     NULL    /* NOTEZ-BIEN: il faut un NULL à la fin du tableau de chaines */
  74. };
  75.  
  76. struct NewCycle newcycle=
  77. {
  78.     203, 17,        /* LeftEdge, TopEdge */
  79.     122, 14,        /* Width, Height */
  80.     LABEL_LEFT|LABEL_HOT_KEY,
  81.     0,
  82.     "C_ycle",        /* Label */
  83.     &font,            /* Fonte */
  84.     text                /* tableau de chaine de caracteres */
  85. };
  86.  
  87. struct NewScroller newscroller=
  88. {
  89.     358, 26,
  90.     106, 11,
  91.     LABEL_ABOVE,
  92.     0,
  93.     "Scroller",
  94.     &font,
  95.     0,        /* Pos = 0 */
  96.     25,        /* Partie visible 25 */
  97.     100        /* Maximum 100 */
  98. };
  99.  
  100.  
  101. struct NewButton newbutton=
  102. {
  103.     22, 17,
  104.     94, 14,
  105.     LABEL_INSIDE|LABEL_HOT_KEY,
  106.     0,
  107.     "_Button",
  108.     &font
  109. };
  110.  
  111.  
  112. struct NewCheck newcheck=
  113. {
  114.     403, 64,
  115.     20, 16,
  116.     LABEL_LEFT|LABEL_HOT_KEY,
  117.     0,
  118.     "_Check",
  119.     &font
  120. };
  121.  
  122.  
  123. struct NewInteger newint=
  124. {
  125.     256, 42,
  126.     72, 14,
  127.     LABEL_RIGHT|LABEL_HOT_KEY,
  128.     STR_CENTER,        /* l'entier est centré dans l'IObject */
  129.     "_Integer",
  130.     &font
  131. };
  132.  
  133.  
  134. struct NewString newstr=
  135. {
  136.     8, 38,
  137.     162, 12,
  138.     LABEL_RIGHT|LABEL_HOT_KEY,
  139.     STR_SINGLEBORD,        /* Bordure simple */
  140.     "_String",
  141.     &font,
  142.     "Welcome to IObject.lib",        /* texte initial */
  143.     60        /* 60 caracteres au maximum */
  144. };
  145.  
  146.  
  147. struct NewButton newtoggle=
  148. {
  149.     331, 102,
  150.     140, 18,
  151.     LABEL_INSIDE|LABEL_HOT_KEY|LABEL_PEN2,
  152.     BT_TOGGLE,        /* le bouton est interrupteur */
  153.     "_Toggle Button",
  154.     &opalfont
  155. };
  156.  
  157.  
  158. /* image pour le bouton */
  159. UWORD imagedata[]=
  160. {
  161.     0x0000, 0x0000, 0x0100, 0x0000,    0x07c0, 0x1ff0, 0x3ff8, 0x3ff8,
  162.     0x7ffc, 0x7efc, 0x7ffc, 0x3ff8,    0x3ff8, 0x1ff0, 0x07c0, 0x0000,
  163.     0x07c0, 0x07c0, 0x0380, 0x07e0,    0x1ff8, 0x3fec, 0x7fde, 0x7fbe,
  164.     0xff7f, 0xffff, 0xffff, 0x7ffe,    0x7ffe, 0x3ffc, 0x1ff8, 0x07e0,
  165. };
  166.  
  167. struct Image ImageButton=
  168. {
  169.     0, 0,
  170.     16, 16,
  171.     2,
  172.     NULL,
  173.     3, 0,
  174.     NULL
  175. };
  176.  
  177. struct NewButton newimgbutton=
  178. {
  179.     381, 80,
  180.     40, 18,
  181.     LABEL_INSIDE,
  182.     BT_IMAGELABEL,        /* le bouton est une image */
  183.     &ImageButton,            /* pointeur vers l'image */
  184.     &font
  185. };
  186.  
  187.  
  188. struct NewTextArea newtextarea=
  189. {
  190.     10, 64,
  191.     312, 57,
  192.     LABEL_ABOVE,
  193.     TAF_CLIP_TEXT,        /* le texte qui dépasse se poursuit à la ligne suivante */
  194.     "TextArea",
  195.     &opalfont,
  196.     1,3,
  197.     1,            /* un pixel entre deux lignes */
  198.     0x03        /* bitplanes 1 et 2 utilisés */
  199. };
  200.  
  201.  
  202. CPTR textarea=NULL;        /* pointeur sur le TextAera */
  203. CPTR EZGadget[ID_MAX + 1]={0};        /* Tableau d'IObject */
  204. UBYTE HotKeyTable[ID_MAX+1];    /* tableau pour gerer les raccourcis clavier */
  205.  
  206.  
  207. /*
  208.  * Cherche un caractère dans un tableau de caracteres
  209.  * Renvoie l'index du caractère dans le tableau ou -1 s'il n'y est pas
  210.  */
  211. WORD FindHotKey(UBYTE hotkey, UBYTE *table, UWORD len)
  212. {
  213.     UWORD ind=0;
  214.  
  215.     while (len--)
  216.     {
  217.         /* toupper si on ne veut pas faire la difference entre majuscule et minuscule */
  218.         if (toupper(*table++) == toupper(hotkey))
  219.             return (ind);
  220.         ind++;
  221.     }
  222.     return (-1);
  223. }
  224.  
  225.  
  226. /*
  227.  * Libere les ressources
  228.  */
  229. VOID clean_exit(void)
  230. {
  231.     COUNT i;
  232.  
  233.     if (window) CloseWindow(window);
  234.     for (i = ID_MIN; i <= ID_MAX; i++)        /* libère tous les object */
  235.         if (EZGadget[i])
  236.             FreeObject(EZGadget[i]);
  237.  
  238.     if (textarea)    FreeTextArea(textarea);
  239.     ExitEasyGadget();        /* IL FAUT appeler ExitEasyGadget() avant de partir */
  240.     if (DiskfontBase) CloseLibrary(DiskfontBase);
  241.     if (GfxBase) CloseLibrary(GfxBase);
  242.     if (IntuitionBase) CloseLibrary(IntuitionBase);
  243.     if (ImageButton.ImageData) FreeMem(ImageButton.ImageData, sizeof(imagedata));
  244.     exit();
  245. }
  246.  
  247.  
  248. main(int ac, char **av)
  249. {
  250.     struct IntuiMessage *message;
  251.     BOOL close_me;
  252.     COUNT i;
  253.  
  254.     /* ouvre l'intuition.library et verifie si elle existe */
  255.     if (!(IntuitionBase = OpenLibrary("intuition.library", 0)))
  256.         clean_exit();
  257.  
  258.     /* ouvre la graphics.library et verifie si elle existe */
  259.     if (!(GfxBase = OpenLibrary("graphics.library", 0)))
  260.         clean_exit();
  261.  
  262.     /* Recopie en CHIP l'image */
  263.     if (ImageButton.ImageData = (UWORD *) AllocMem(sizeof(imagedata), MEMF_CHIP))
  264.         memcpy(ImageButton.ImageData, imagedata, sizeof(imagedata));
  265.     else
  266.         clean_exit();
  267.  
  268.     /* si demo est lancé sans arguments, on ouvre la diskfont.library */
  269.     if (ac == 1)
  270.         DiskfontBase = OpenLibrary("diskfont.library", 0);
  271.  
  272.     /* Si la diskfont.library est ouverte on utilise les fontes demandées */
  273.     /* sinon on utilise la Topaz 8 par defaut. */
  274.  
  275.     /* On ouvre la fenetre */
  276.     window = (struct Window *) OpenWindow( &new_window );
  277.  
  278.     /* fenetre ouverte ? */
  279.     if(!window)
  280.         clean_exit();
  281.  
  282.     /* Initialiser IObject.lib */
  283.     if (!InitEasyGadget())
  284.         clean_exit();
  285.  
  286.     /* cree le TextArea, on s'en va si il y a une erreur */
  287.     if (!(textarea = CreateTextArea(&newtextarea, window->RPort)))
  288.         clean_exit();
  289.  
  290.     /* Creer les objets */
  291.     EZGadget[ID_CYCLE]        = CreateCycle(&newcycle, ID_CYCLE);
  292.     EZGadget[ID_SCROLLER] = CreateScroller(&newscroller, ID_SCROLLER);
  293.     EZGadget[ID_BUTTON]     = CreateButton(&newbutton, ID_BUTTON);
  294.     EZGadget[ID_CHECK]        = CreateCheck(&newcheck, ID_CHECK);
  295.     EZGadget[ID_INTEGER]    = CreateInteger(&newint, ID_INTEGER);
  296.     EZGadget[ID_STRING]     = CreateString(&newstr, ID_STRING);
  297.     EZGadget[ID_TGBUTTON] = CreateButton(&newtoggle, ID_TGBUTTON);
  298.     EZGadget[ID_IMGBUTTON] = CreateButton(&newimgbutton, ID_IMGBUTTON);
  299.  
  300.     /* Verifier que les objects ont été crées */
  301.     for (i = ID_MIN; i <= ID_MAX; i++)
  302.         if (EZGadget[i] == NULL)
  303.             clean_exit();
  304.  
  305.     /* ajouter les objets à la fenetre et les afficher */
  306.     for (i = ID_MIN; i <= ID_MAX; i++)
  307.         if (EZGadget[i])
  308.         {
  309.             AddObjectToWindow(EZGadget[i], window, NULL);
  310.             DisplayObject(EZGadget[i]);
  311.         }
  312.  
  313.     /* affiche un message de bienvenue */
  314.     TAPuts(textarea, "Welcome in IObject.lib");
  315.  
  316.     /* remplit le tableau pour gerer les raccourcis-clavier */
  317.     for (i = ID_MIN; i <= ID_MAX; i++)
  318.         HotKeyTable[i] = GetHotKey(EZGadget[i]);
  319.  
  320.     /* l'entier doit être compris entre -5000 et 5000 */
  321.     ModifyObject(EZGadget[ID_INTEGER], 0, -5000, 5000);
  322.  
  323.     /* On reste dans la boucle tant qu'on a pas cliqué dans le gadget de fermeture */
  324.     close_me = FALSE;
  325.     while(close_me == FALSE)
  326.     {
  327.         /* attendre un évenement */
  328.         Wait(1 << window->UserPort->mp_SigBit);
  329.  
  330.         /* Recuperer l'IntuiMessage */
  331.         while(message = (struct IntuiMessage *) GetMsg(window->UserPort))
  332.         {
  333.             struct IntuiMessage msg;
  334.             BOOL changed;
  335.             CPTR obj;
  336.             UWORD ID;
  337.  
  338.             changed = FALSE;
  339.             msg = *message;            /* recopier le message */
  340.             ReplyMsg(message);    /* Repondre au message */
  341.  
  342.             /* Traiter l'evenement */
  343.             switch( msg.Class )
  344.             {
  345.                 case CLOSEWINDOW:    /* CloseWindow gadget => on s'en va */
  346.                     close_me=TRUE;
  347.                     break;
  348.  
  349.                 case VANILLAKEY:    /* Raccourcis-clavier */
  350.                     if ((ID = FindHotKey(msg.Code, HotKeyTable, ID_MAX+1)) != -1)
  351.                     {
  352.                         obj = EZGadget[ID];
  353.                         ActivateObject(obj);
  354.                         changed = TRUE;
  355.                     }
  356.                     break;
  357.  
  358.                 default:
  359.                     if (obj = FindObjectMsg(&msg))
  360.                     {
  361.                         ID = GetObjectID(obj);
  362.                         changed = SendMsgToObject(obj, &msg);
  363.                     }
  364.                     break;
  365.             }
  366.  
  367.             if (changed)
  368.             {
  369.                 switch (ID)
  370.                 {
  371.                     case ID_CYCLE:
  372.                         TAPrintf(textarea, "Option %s selected.\n", text[ObjectValue(obj)]);
  373.                         break;
  374.                     case ID_SCROLLER:
  375.                         TAPrintf(textarea, "Position = %ld.\n", ObjectValue(obj));
  376.                         break;
  377.                     case ID_BUTTON:
  378.                         TAPuts(textarea, "Button pressed.");
  379.                         break;
  380.                     case ID_CHECK:
  381.                         if (ObjectValue(obj))
  382.                             TAPuts(textarea, "Check is marked.");
  383.                         break;
  384.                     case ID_INTEGER:
  385.                         TAPrintf(textarea, "Integer Value is %ld.\n", ObjectValue(obj));
  386.                         break;
  387.                     case ID_STRING:
  388.                         TAPrintf(textarea, "You have entered the text: %s.\n", ObjectValue(obj));
  389.                         break;
  390.                     case ID_TGBUTTON:
  391.                         TAPrintf(textarea, "Button is %s.\n", ObjectValue(obj) ? "On" : "Off");
  392.                         break;
  393.                     case ID_IMGBUTTON:
  394.                         TAPuts(textarea, "Image Button pressed");
  395.                         break;
  396.                 }
  397.             }
  398.         }
  399.     }
  400.     clean_exit();
  401. }
  402.